import axios from "axios";
import { Message } from "element-ui";
import ApiConfig from "./api-config";
import { getToken, removeToken } from "@/utils/auth";

const reLogin = () => {
	if(process.client){
		window.$nuxt.$router.push({ path: "/login" });
	}
};

const SYSTEM_ERROR_MSG = "网络卡顿，请稍后再试！";
const service = axios.create({
	baseURL: ApiConfig.shopBasicUrl,
	// withCredentials: true, // send cookies when cross-domain requests
	headers: { "Content-Type": "application/x-www-form-urlencoded" },
	timeout: 50000, // request timeout
});

let _insertActionRegexp = new RegExp("post");
let _updateActionRegexp = new RegExp("post");
// request interceptor
service.interceptors.request.use(
	(config) => {
		if (
			_insertActionRegexp.test(config.method) ||
			_updateActionRegexp.test(config.method)
		) {
			config.transformRequest = [];
		} else {
			config.transformRequest = [
				function(data) {
					let ret = "";
					for (let it in data) {
						ret +=
							encodeURIComponent(it) +
							"=" +
							encodeURIComponent(data[it]) +
							"&";
					}
					return ret;
				},
			];
		}

		if (getToken()) {
			config.headers["token"] = getToken();
		}
		return config;
	},
	(error) => {
		return Promise.reject(error);
	}
);

// response interceptor
service.interceptors.response.use(
	(response) => {
		// 成功
		if (
			response &&
			response.data &&
			response.data.code &&
			response.data.code == "000000" &&
			response.data.success
		) {
			let resSure = response.data.code;
			if ("000000" === resSure) {
				return response.data || response;
			}
		}
		// 异常
		if (
			response &&
			response.data &&
			response.data.code &&
			response.data.code !== "000000"
		) {
			let res = response.data.code;
			let msg = response.data.message;
			if ("111000" === res) {
				msg = msg || "用户注册异常";
			} else if ("100000" === res) {
				msg = msg || "系统错误";
			} else if ("112200" === res) {
				msg = msg || "登录已过期，请重新登录";
				// sessionStorage.removeItem("userInfo");
				removeToken();
				reLogin();
				// 
			} else if ("112000" === res) {
				msg = msg || "用户登录异常，请登录";
				// sessionStorage.removeItem("userInfo");
				removeToken();
				reLogin();
			} else if ("112001" === res) {
				msg = msg || "用户未登录，请登录";
				// sessionStorage.removeItem("userInfo");
				removeToken();
				reLogin();
			} else if ("113000" === res) {
				msg = msg || "访问权限异常";
			} else if ("114000" === res) {
				msg = msg || "用户请求参数错误";
			} else if ("115000" === res) {
				msg = msg || "用户请求服务异常";
			} else if ("116000" === res) {
				msg = msg || "用户资源异常";
			} else if ("117000" === res) {
				msg = msg || "用户上传文件异常";
			} else if ("121000" === res) {
				msg = msg || "系统执行超时";
			} else if ("122000" === res) {
				msg = msg || "系统容灾功能被触发";
			} else if ("123000" === res) {
				msg = msg || "系统资源异常";
			} else if ("131000" === res) {
				msg = msg || "中间件服务出错";
			}
			Message.closeAll();
			Message.error(`${msg}`);
			return Promise.reject(response);
		} else {
			return response.data || response;
		}
	},
	(error) => {
		let msg = SYSTEM_ERROR_MSG;
		if (error && error.request && error.request.status == 401) {
			msg = "登录已过期，请重新登录";
			reLogin();
		}
		if (error && error.request && error.request.status == 400) {
			msg = "网络卡顿，请稍后再试！";
		}
		Message.closeAll();
		Message.error(msg);
		return Promise.reject(error);
	}
);

export default service;
